<?php

namespace Trawind\ConfigCenter\Helpers;


use Trawind\Helpers\UserHelper;
use Illuminate\Support\Facades\Redis;
use Trawind\ConfigCenter\Middleware\ConfigVersion;
use Trawind\Remotes\Repositories\Account\Config\CommonRepositoryEloquent as RemoteCommonRepositoryEloquent;
use Trawind\Remotes\Repositories\Account\Config\UserRepositoryEloquent as RemoteUserRepositoryEloquent;
use TrawindCloud\Repositories\Account\Config\CommonRepositoryEloquent as LocalCommonRepositoryEloquent;
use TrawindCloud\Repositories\Account\Config\UserRepositoryEloquent as LocalUserRepositoryEloquent;

class ConfigHelper
{

    //全局配置(系统配置)
    const SYSTEM_DEFAULT = ['system', 'default'];

    //功能配置
    const FUNCTION_ACCOUNT = ['function', 'account'];
    const FUNCTION_FINANCE = ['function', 'finance'];
    const FUNCTION_PRODUCT = ['function', 'product'];
    const FUNCTION_PURCHASE = ['function', 'purchase'];
    const FUNCTION_SALE = ['function', 'sale'];
    const FUNCTION_INVENTORY = ['function', 'inventory'];

    //产品配置
    const PRODUCT_PRODUCT = ['product', 'product'];
    const PRODUCT_SAMPLE = ['product', 'sample'];
    const PRODUCT_PRODUCT_PUBLICITY = ['product', 'product_publicity'];
    const PRODUCT_PRODUCT_RECOMMEND = ['product', 'product_recommend'];
    const PRODUCT_ITEM = ['product', 'item'];

    //存储配置
    const STORAGE_IMAGE = ['storage', 'image'];
    const STORAGE_AUDIO = ['storage', 'audio'];
    const STORAGE_VIDEO = ['storage', 'video'];
    const STORAGE_DOCUMENTATION = ['storage', 'documentation'];
    const STORAGE_TEMPLATE = ['storage', 'template'];
    const STORAGE_OTHER = ['storage', 'other'];

//
    const PURCHASE_PURCHASE = ['purchase', 'purchase'];
    /**
     * @return string
     */
    public static function userKey(): string
    {
        //$userInfo = UserHelper::userInfo();
        $tenantId = UserHelper::tenantId('tenant_id');
        $roleId = UserHelper::roleId('role_id');
        $userId = UserHelper::userId('user_id');

        return "config:user:$tenantId:$roleId:$userId";
    }


    public static function commonKey(string $domain = self::SYSTEM_DEFAULT[0], string $module = self::SYSTEM_DEFAULT[1])
    {
        $tenantId = UserHelper::tenantId('tenant_id');
        return "config:common:$tenantId:$domain:$module";
    }


    public static function resolve($key = null, $default = null)
    {
        $config = app()->has(ConfigVersion::USER_CONFIG) ? app(ConfigVersion::USER_CONFIG) : [];

        if (!is_null($key)) {
            return $config[$key] ?? $default;
        }

        return $config;
    }


    public static function userConfig()
    {
//        if (class_exists(LocalUserRepositoryEloquent::class)) {
//            $userConfig = app(LocalUserRepositoryEloquent::class)->getUserConfig();
//        } else {
//            $userConfig = app(RemoteUserRepositoryEloquent::class)->getUserConfig();
//        }
//
//        return $userConfig;

        return [
            'language' => 'zh_CN',
            'time_zone' => 'tz68',
            'page_size' => 50,
            'show_external_id' => 1,
            'show_internal_id' => 1,
            'department_approval_level' => 1,
        ];
    }


    public static function commonConfig(array $constant)
    {
        [$service, $module] = $constant;

        if (class_exists(LocalCommonRepositoryEloquent::class)) {
            $config = app(LocalCommonRepositoryEloquent::class)
                ->getData($service, $module);
        } else {
            $config = app(RemoteCommonRepositoryEloquent::class)
                ->getData($service, $module);
        }

        $data = $config['data'] ?? [];
        return array_combine(array_column($data, 'key'), array_column($data, 'value'));
    }

}
